package com.hc.controller;

import com.hc.bean.Pager;
import com.hc.bean.ProductInfo;
import com.hc.bean.Type;
import com.hc.service.ProductInfoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productinfo")
public class ProductInfoController {
    @Autowired
    private ProductInfoService productInfoService;

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> getProductList(ProductInfo productInfo, Integer page, Integer rows) {
        Map params = new HashMap();
        Pager pager = new Pager();
        pager.setPage(page);
        pager.setPageRows(rows);    //每页记录行数
        params.put("productInfo", productInfo);
        System.out.println("=====================controller"+productInfo);
        params.put("pager", pager);
        List<ProductInfo> productInfoList = productInfoService.getProductList(params);
        int count = productInfoService.getCountByParam(params);
        Map resultMap = new HashMap();
        resultMap.put("total", count);
        resultMap.put("rows", productInfoList);
        return resultMap;
    }

    @RequestMapping("/addProduct")
    @ResponseBody
    public Map<String, Object> addProduct(ProductInfo productInfo, MultipartFile file, HttpServletRequest request) throws IOException {
//        System.out.println("=============productInfo"+productInfo);
//        System.out.println("文件名:" + file.getOriginalFilename());
        //获取文件名
        String fileName = file.getOriginalFilename();
        //获取服务器端某个文件的绝对路径
        String filePath = request.getRealPath("product_images");
//        System.out.println(filePath);
        if (fileName != null && !"".equals(fileName.trim())) {
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdirs();
            }
            filePath = filePath + "/" + fileName;
            file.transferTo(new File(filePath));
            /*把文件名作为路径传入数据库*/
            productInfo.setPic(fileName);
        }
        Map resultMap = new HashMap();
        if (productInfoService.addProductInfo(productInfo)) {
            /*前端的true使用的是字符串,所有后端必须使用字符串*/
            resultMap.put("success", "true");
            resultMap.put("message", "商品信息添加成功!");
        } else {
            resultMap.put("success", "false");
            resultMap.put("message", "商品信息添加失败!");
        }
        return resultMap;
    }

    @RequestMapping("/updateProduct")
    @ResponseBody
    public Map<String, Object> updateProduct(ProductInfo productInfo, MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println("============productInfo" + productInfo);
//        System.out.println("文件名:" + file.getOriginalFilename());
        //获取文件名
        String fileName = file.getOriginalFilename();
        //获取服务器端某个文件的绝对路径
        String filePath = request.getRealPath("product_images");
//        System.out.println("================================="+filePath);
        if (fileName != null && !"".equals(fileName.trim())) {
            File file1 = new File(filePath);
            if (!file1.exists()) {
                file1.mkdirs();
            }
            filePath = filePath + "/" + fileName;
            file.transferTo(new File(filePath));
            /*把文件名作为路径传入数据库*/
            productInfo.setPic(fileName);
        }
        Map resultMap = new HashMap();
        if (productInfoService.updateProductInfo(productInfo)) {
            resultMap.put("success", "true");
            resultMap.put("message", "商品信息修改成功!");
        } else {
            resultMap.put("success", "false");
            resultMap.put("message", "商品信息修改失败!");
        }
        return resultMap;
    }

    @RequestMapping("/changeProductStatus")
    @ResponseBody
    /*
     * @RequestParam主要应用在Controller层
     * 前端提交的form表单数据中的属性名和方法中的参数名不一致时 ，springMVC就无法自动封装参数,这时候需要使用这个注解
     * */
    public Map<String, Object> changeProductStatus(String ids, Integer flag) {
//        System.out.println("标志:======="+flag+"=="+ids);
        ids = ids.substring(0, ids.length() - 1);
//        System.out.println(ids);
        Map resultMap = new HashMap();
        if (productInfoService.changeProductStatus(flag, ids)) {
            resultMap.put("success", "true");
            resultMap.put("message", "商品状态改成功!");
        } else {
            resultMap.put("success", "false");
            resultMap.put("message", "商品状态改失败!");
        }
        return resultMap;
    }

    @RequestMapping("/getOnSaleProduct")
    @ResponseBody
    public List<ProductInfo> getOnSaleProduct() {
        return productInfoService.getOnSaleProduct();
    }

    @RequestMapping("/getPriceById")
    @ResponseBody
    public Double getPriceById(Integer pid) {
        if (pid!=null){
            return productInfoService.getPriceById(pid);
        }
        return null;
    }
}
